package net.jiastudy.biz.datasync.impl;

import net.jiastudy.biz.datasync.DataSyncService;
import net.jiastudy.biz.datasync.DataSyncStrategy;
import net.jiastudy.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class DataSyncServiceImpl implements DataSyncService {

	@Autowired
	private Map<String, DataSyncStrategy> dataSyncStrategys = new ConcurrentHashMap<>();

	@Value("${customer.code}")
	private String customerCode;

	@Override
	public boolean syncDepts() {
		return doStrategy().syncDepts();
	}

	@Override
	public boolean syncTeacher() {
		return doStrategy().syncTeacher();
	}

	@Override
	public boolean syncAll() {
		this.syncDepts();
		this.syncTeacher();

		return true;
	}

	public DataSyncStrategy doStrategy() {
		String type = this.customerCode + "DataSyncStrategy";
		DataSyncStrategy strategy = this.dataSyncStrategys.get(type);
		if (null == strategy) {
			throw new ServiceException(type + "数据同步策略不存在");
		}
		return strategy;
	}

}
